//
//  OperatorView.swift
//  swiftStudy
//
//  Created by ext.zhangjunfei on 2021/6/29.
//

import SwiftUI

struct OperatorView: View {
    let code_1: String
    let code_2: String
    let code_3: String
    let code_4: String
    
    
    init() {
        code_1 =
            """
            //可空类型
            var q:Int?
            var value:Int

            //判断为空并打印
            if q != nil {
                value = q!
            }else{
                value = 0
            }
            print(value)

            //使用三元运算符
            value = (q != nil) ? q! : 0
            print(value)

            //使用空合并运算符
            value = q ?? 0
            print(value)
            """
        code_2 =
            """
            //之前表示区间的操作
            var index = 5
            if index>0 && index<10 {
                print("在区间(0..10)的范围内")
            }

            //使用区间运算符,..<半开区间
            if 0..<10 ~= index {
                print("在区间(0..<10)的范围内")
            }

            //使用区间运算符,...闭区间
            if 0...10 ~= index {
                print("在区间(0...10)的范围内")
            }
            """
        code_3 =
            """
            //for-in循环
            for index in 1...5{
                print(index)
            }

            //使用集合进行循环
            var array = [1,2,3,4,5]
            for i in array {
                print(i)
            }
            """
        code_4 =
            """
            var score = 77
            switch score {
            case 0..<60:
                print("不及格")
            case 60..<80:
                print("良好")
            case 80..<95:
                print("优秀")
            case 95,96,97,98,99,100:
                print("学霸")
            default:
                print("未参加考试")
            }
            """
    }
    
    var body: some View {
        ScrollView(.vertical){
            LazyVStack(alignment: .leading, spacing: 5){
                
                Section(header: SectionHeaderView(title: "运算符")) {
                    CommonTextView(content: "Swift有两种特殊的运算符：空合并运算符、区间运算符")
                }
                
                Section(header: SectionHeaderView(title: "空合并运算符")) {
                    CodeView(code: code_1).padding(10)
                }
                Section(header: SectionHeaderView(title: "区间运算符")) {
                    CodeView(code: code_2).padding(10)
                }
                Section(header: SectionHeaderView(title: "for-in循环")) {
                    CodeView(code: code_3).padding(10)
                }
                Section(header: SectionHeaderView(title: "switch-case循环")) {
                    CodeView(code: code_4).padding(10)
                }
            }
        }
    }
}

struct _OperatorView_Previews: PreviewProvider {
    static var previews: some View {
        OperatorView()
    }
}
